package cn.com.callback

import cn.com.elite.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import java.text.SimpleDateFormat

/**
 * @Title: autoSyncTradeDay* @author: 同步平安期货核心交易日历
 */

class autoSyncTradeDay implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(autoSyncTradeDay.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {

        long startTime = System.currentTimeMillis();
        log.debug("同步平安期货核心交易日历-开始时间:" + startTime);

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sync_time = simpleDateFormat.format(new Date());
            int dealNum = 0;
            String dealSQL = "";

            List befTimeList = eliteBean.chaxun_sql("select sync_time From t_Calendar group by sync_time order by sync_time desc limit 3,1", "dataSource");
            if (befTimeList.size() > 0) {
                Map befTimeMap = befTimeList.get(0);
                String async_time = befTimeMap.get("sync_time");
                dealNum = eliteBean.update("Delete From t_Calendar Where Sync_Time <= '" + async_time + "'", "dataSource");
                log.debug("删除3次前的历史同步数据-" + dealNum);
            } else {

                dealSQL = '''Delete From t_Calendar Where Sync_Time < Date_Add(Now(), Interval -3 Day)''';
                dealNum = eliteBean.update(dealSQL, "dataSource");
                log.debug("删除3天前的历史同步数据-" + dealNum);
            }

            dealSQL = '''Insert Into t_Calendar (Exchangeid, Day, Dat, Wrk, Tra, Sun, Mon, Tue, Wed, Thu, Fri, Sat, Str, Tal, Spr, Sync_Time)
                              Select R1.Exchangeid, R1.Day, R1.Dat, R1.Wrk, R1.Tra, R1.Sun, R1.Mon, R1.Tue, R1.Wed, R1.Thu, R1.Fri, R1.Sat, R1.Str, R1.Tal, R1.Spr, \'''' + sync_time + '''\'
                                From Dws.t_Calendar R1
                               Where R1.Day >= Date_Format(Now(), '%Y%m%d') And R1.Day <= Date_Format(Date_Add(Now(), Interval 365 Day), '%Y%m%d')''';
            dealNum = eliteBean.update(dealSQL, "dataSource");

            dealSQL = '''Select Date_Format(R1.Day, '%Y-%m-%d') As Ndate, If(R1.Tra = 1, 'PA0001', 'PA0000') As Ndatetype, \'''' + sync_time + '''\' As Sync_Time
                            From (Select Distinct R1.Day, R1.Tra
                                    From t_Calendar R1
                                   Where R1.Day >= Date_Format(Now(), '%Y%m%d')
                                     And R1.Day <= Date_Format(Date_Add(Now(), Interval 365 Day), '%Y%m%d')
                                     And R1.Sync_Time <= \'''' + sync_time + '''\'
                                     And R1.Tra = 1
                                  Union All
                                  Select Distinct R1.Day, R1.Tra
                                    From t_Calendar R1
                                    Left Join t_Calendar R2
                                      On R1.Day = R2.Day
                                     And R2.Tra = 1
                                     And R2.Day >= Date_Format(Now(), '%Y%m%d')
                                     And R2.Day <= Date_Format(Date_Add(Now(), Interval 365 Day), '%Y%m%d')
                                     And R2.Sync_Time <= \'''' + sync_time + '''\'
                                   Where R1.Day >= Date_Format(Now(), '%Y%m%d')
                                     And R1.Day <= Date_Format(Date_Add(Now(), Interval 365 Day), '%Y%m%d')
                                     And R1.Sync_Time <= \'''' + sync_time + '''\'
                                     And R1.Tra = 0
                                     And R2.Day Is Null) R1
                           Order By R1.Day''';
            List dealList = eliteBean.chaxun_sql(dealSQL, "dataSource");
            for (int i = 0; i < dealList.size(); i++) {

                Map dealListMap = dealList.get(i);
                String Ndate = dealListMap.get("Ndate");
                String Ndatetype = dealListMap.get("Ndatetype");
                String Sync_Time = dealListMap.get("Sync_Time");

                // 检查是否已经存在
                List Promote_OpenList = eliteBean.chaxun_sql("Select * From c_Freedate Where Date_Format(Freedate, '%Y-%m-%d') = '" + Ndate + "' And Datetype = '" + Ndatetype + "'", "dataSource");
                if (Promote_OpenList.size() > 0) {
                    // 已经存在更新
                    dealNum = eliteBean.update("Update c_Freedate Set Datetype = '" + Ndatetype + "', Sync_Time = '" + Sync_Time + "' Where Date_Format(Freedate, '%Y-%m-%d') = '" + Ndate + "'", "dataSource");
                    log.debug("已经存在更新-" + dealNum);
                } else {
                    // 不存在新建
                    dealNum = eliteBean.update("Insert Into c_Freedate (Freedate, Datetype, Sync_Time) Values ('" + Ndate + "', '" + Ndatetype + "', '" + Sync_Time + "')", "dataSource");
                    log.debug("不存在新建-" + dealNum);
                }
            }

            dealSQL = '''Delete From c_Freedate
                         Where Freedate >= Date_Format(Now(), '%Y%m%d')
                           And Freedate <= Date_Format(Date_Add(Now(), Interval 365 Day), '%Y%m%d')
                           And Sync_Time != \'''' + sync_time + '''\'
                           And Datetype In ('PA0001', 'PA0000')''';

            dealNum = eliteBean.update(dealSQL, "dataSource");
            log.debug("删除之前同步，本次未涉及的数据-" + dealNum);

            long endTime = System.currentTimeMillis();
            log.debug("同步平安期货核心交易日历-总耗时:" + (endTime - startTime) + " 处理结果：" + dealNum.toString());

        } catch (Exception e) {
            log.error("同步平安期货核心交易日历异常!", e);
        }
    }
}